package com.myproject.utils;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/*
* 5.7版本mysql-connector-java可以使用5.1.44版本jar包
* 8.0版本mysql-connector-java要使用8.0版本jar包
* */
public class JDBC {
    public static void main(String[] args) throws IOException {

        InputStream resourceAsStream = ClassLoader.getSystemClassLoader()
                .getResourceAsStream("config.properties");
        Properties properties = new Properties();
        properties.load(resourceAsStream);
        String ip = properties.getProperty("ip");
        String db = properties.getProperty("db");
        String name = properties.getProperty("name");
        String password = properties.getProperty("password");

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Connection connection = null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://"+ip+":3306/"+db+"?characterEncoding=utf8&useSSL=false",name,password);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        String sql = "select version()";
        Statement statement = null;
        try {
            statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()){
                String version = rs.getString("version()");
                System.out.println("mysql version: "+version);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
